{{template Elements/header_content}}
<style>
body{background:#FFF;}
</style>
    <div class="row">
    	<div class="col-xs-12">
      		<div class="wx-cover-item noborder">
	       		{{$this->Html->image($WxPromotion['WxPromotion']['coverimg'])}}
				<h4>{{$WxPromotion['WxPromotion']['name']}}</h4>
			</div>
			<div class="wx-text-item"><i class="fa fa-clock-o"></i> {{$WxPromotion['WxPromotion']['start_time']}} ~ {{$WxPromotion['WxPromotion']['end_time']}} </div>
			
			<a id="btn-coupon" class="btn btn-primary btn-block" href="javascript:void(0)" onclick="getCoupon('{{$WxPromotion['WxPromotion']['id']}}')">Claim</a>
			
			<div class="alert alert-info coupon-info text-center hidden"></div>
			
			<div class="wx-text-item noborder">{{$WxPromotion['WxPromotion']['content']}}</div>
			<div class="alert alert-info coupon-info text-center hidden"></div>
			
			<a id="btn-redeem" class="btn btn-warning btn-block hidden" href="javascript:void(0)" >Redeem</a>
			
			
		</div>
	</div>
	
<script>

function getCoupon(id){
	$.ajax({
		async:true, 
		type:'get',
		url: BASEURL+"/wx_coupons/getCoupon/"+id,
		success: function (request){
			if(request.ret == 0){
				var coupon = request.coupon_code;
				$.cookie('coupon_{{$WxPromotion['WxPromotion']['id']}}',coupon);
				$('#btn-coupon').hide();
				$('.coupon-info').html(coupon);
				$('.coupon-info').removeClass('hidden');
				$('.coupon-info').removeClass('alert-warning').addClass('alert-info').css('text-decoration','none');
				//$('#btn-redeem').addClass('hidden');
				$('#btn-redeem').removeClass('hidden');
			}
		},
		dataType:"json"
	});
	return false;
}

$(function(){
	var coupon = $.cookie('coupon_{{$WxPromotion['WxPromotion']['id']}}');
	if(coupon && coupon != 'null'){
		$('#btn-coupon').hide();
		$('.coupon-info').html(coupon);
		$('.coupon-info').removeClass('hidden');
		$('#btn-redeem').removeClass('hidden');
	}
	$('#btn-redeem').click(function(){
		var coupon = $.cookie('coupon_{{$WxPromotion['WxPromotion']['id']}}');
		if(!coupon || coupon == 'null'){
			alert('Please claim at first.');
			return false;
		}
		if(!confirm('Staff use only, are you sure to redeem?')){
			return false;
		}
		$.ajax({
			async:true, 
			type:'get',
			url: BASEURL+"/wx_coupons/redeem/{{$WxPromotion['WxPromotion']['id']}}/"+coupon,
			success: function (request){
					if(request.ret == 0){
						$.cookie('coupon_{{$WxPromotion['WxPromotion']['id']}}',null);
						$('#btn-coupon').html('Reclaim').show();
						$('#btn-redeem').addClass('hidden');
						$('.coupon-info').removeClass('alert-info').addClass('alert-warning').css('text-decoration','line-through');
					}
			},
			dataType:"json"
		});
	});
})
</script>
